package com.hex.ds.hdtp.core.app.data.transfer.converter;

import com.hex.ds.hdtp.core.app.common.entity.BasicContrastInfo;
import com.hex.ds.hdtp.core.app.common.entity.BasicTransferInfo;
import com.hex.ds.hdtp.core.app.data.table.dto.response.TableTransferFieldMapResponseDto;
import com.hex.ds.hdtp.core.app.data.transfer.dto.DataTransferSqlStructureDto;
import com.hex.ds.hdtp.core.app.data.transfer.dto.response.DataTransferListResponseDto;
import com.hex.ds.hdtp.core.inf.metadata.po.SourceInfoPo;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Package com.hex.ds.hdtp.core.app.data.transfer.converter
 * @ClassName IDataTransferSqlStructureConverter
 * @Description 数据迁移SQL结构对象转换
 * @Author gj.xu
 * @Date 2024/3/4 18:35
 * @Version v2.0
 **/
@Service
@Mapper(componentModel = "spring")
public interface IDataTransferSqlStructureConverter {

    @Mapping(target = "dataTransferTaskPkId", source = "dataTransferListResponseDto.dataTransferTaskPkId")
    @Mapping(target = "dataTransferListPkId", source = "dataTransferListResponseDto.pkId")
    @Mapping(target = "sourceConnPkId", source = "dataTransferListResponseDto.sourceConnPkId")
    @Mapping(target = "sourceDatabaseType", source = "sourceInfoPo.sourceType")
    @Mapping(target = "targetDatabaseType", source = "targetInfoPo.sourceType")
    @Mapping(target = "sourceDatabase", source = "dataTransferListResponseDto.sourceDatabaseName")
    @Mapping(target = "sourceTable", source = "dataTransferListResponseDto.sourceTableName")
    @Mapping(target = "targetConnPkId", source = "dataTransferListResponseDto.targetConnPkId")
    @Mapping(target = "targetDatabase", source = "dataTransferListResponseDto.targetDatabaseName")
    @Mapping(target = "targetTable", source = "dataTransferListResponseDto.targetTableName")
    @Mapping(target = "sourceConfList", source = "sourceInfoPo.confTemplateList")
    @Mapping(target = "targetConfList", source = "targetInfoPo.confTemplateList")
    @Mapping(target = "metadatas", source = "metadatas")
    @Mapping(target = "whereCondition",source = "dataTransferListResponseDto.whereCondition")
    DataTransferSqlStructureDto toDto(DataTransferListResponseDto dataTransferListResponseDto,
                                      SourceInfoPo sourceInfoPo,SourceInfoPo targetInfoPo,
                                      List<TableTransferFieldMapResponseDto> metadatas);

    @Mapping(target = "sourceCode", source = "sourceInfo.sourceCode")
    @Mapping(target = "sourceSchema", source = "dataTransferSqlStructureDto.sourceDatabase")
    @Mapping(target = "sourceTable", source = "dataTransferSqlStructureDto.sourceTable")
    @Mapping(target = "contrastCode", source = "targetInfo.sourceCode")
    @Mapping(target = "contrastSchema", source = "dataTransferSqlStructureDto.targetDatabase")
    @Mapping(target = "contrastTable", source = "dataTransferSqlStructureDto.targetTable")
    BasicContrastInfo toBasicContrastInfo(DataTransferSqlStructureDto dataTransferSqlStructureDto,
                                          SourceInfoPo sourceInfo, SourceInfoPo targetInfo);

    /**
     * 转换迁移基础实体类
     * @param dataTransferList
     * @param sourceInfo
     * @param targetInfo
     * @return
     */
    @Mapping(target = "sourceCode", source = "sourceInfo.sourceCode")
    @Mapping(target = "sourceSchema", source = "dataTransferList.sourceDatabaseName")
    @Mapping(target = "sourceTable", source = "dataTransferList.sourceTableName")
    @Mapping(target = "targetCode", source = "targetInfo.sourceCode")
    @Mapping(target = "targetSchema", source = "dataTransferList.targetDatabaseName")
    @Mapping(target = "targetTable", source = "dataTransferList.targetTableName")
    BasicTransferInfo toCrossDbContrastCommonDto(DataTransferListResponseDto dataTransferList,
                                                 SourceInfoPo sourceInfo, SourceInfoPo targetInfo);
}
